#!/bin/sh
# PCP QA Test No. 1110
# Exercise the HAProxy PMDA - install, remove and values.
#
# Copyright (c) 2017 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmda" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmda module not installed"

[ -f $PCP_PMDAS_DIR/haproxy/connect ] || _notrun "haproxy PMDA not installed"

# from the PMDA's Install script
eval `$PCP_PYTHON_PROG $PCP_PMDAS_DIR/haproxy/connect --config`
[ -n "$user" ] || _notrun "haproxy PMDA's connect script failed to set \$user"
[ -n "$socket" ] || _notrun "haproxy PMDA's connect script failed to set \$socket"
[ -S "$socket" ] && _notrun "haproxy stats socket already exists; haproxy service running?"

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
$sudo rm -rf $tmp.* $seq.full

# cull these "first time ever run" warnings ...
# [Wed Oct  9 09:07:50] pmdahaproxy(29369) Error: pmdaCacheOp: $PCP_VAR_DIR/config/pmda/145.0: empty file?
#
install_filter()
{
    sed \
	-e '/Error: pmdaCacheOp: .* empty file?/d' \
    # end
}

pmda_filter()
{
    sed \
	-e "s/\[.* or /\[X or /g" \
    #end
}

pmda_remove()
{
    echo
    echo "=== remove haproxy agent ==="
    $sudo ./Remove >$tmp.out 2>&1
    _filter_pmda_remove <$tmp.out
}

pmda_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/haproxy
    $sudo ./Remove >/dev/null 2>&1

    cat <<EOF >$tmp.config
[pmda]
user = $user
socket = $socket
EOF
    echo "pmdahaproxy config:" >> $here/$seq.full
    cat $tmp.config >> $here/$seq.full

    [ -f $PCP_PMDAS_DIR/haproxy/haproxy.conf ] && \
    $sudo cp $PCP_PMDAS_DIR/haproxy/haproxy.conf $tmp.backup
    $sudo cp $tmp.config $PCP_PMDAS_DIR/haproxy/haproxy.conf

    echo
    echo "=== haproxy agent installation ==="
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out | _filter_pmda_install | install_filter
}

pmda_cleanup()
{
    $sudo $signal -s KILL $spid >/dev/null 2>&1
    $sudo rm -f $socket >/dev/null 2>&1
    if [ -f $tmp.backup ]; then
        $sudo cp $tmp.backup $PCP_PMDAS_DIR/haproxy/haproxy.conf
        $sudo rm $tmp.backup
    else
        $sudo rm -f $PCP_PMDAS_DIR/haproxy/haproxy.conf
    fi
    # note: _restore_auto_restart pmcd done in _cleanup_pmda()
    _cleanup_pmda haproxy
}

_prepare_pmda haproxy
trap "pmda_cleanup; exit \$status" 0 1 2 3 15

_stop_auto_restart pmcd

# real QA test starts here
$sudo mkdir -p $(dirname $socket)
$sudo chmod 0755 $(dirname $socket)
$sudo $python $here/haproxy/haproxy_stats_server.python $socket &
spid=$($sudo ps -fwaux 2>/dev/null | grep python | awk '/pcp\/testsuite/ {print $2}')
disown > /dev/null 2>&1 || :
pmsleep 0.5	# give server a chance to start
$sudo chcon system_u:object_r:haproxy_var_lib_t:s0 $(dirname $socket) $socket >/dev/null 2>&1 || :
pmda_install
echo

echo "=== report metric values ==="
metrics=`pminfo haproxy | LC_COLLATE=POSIX sort`
pminfo -dfmtT $metrics 2>&1 \
| $here/src/sortinst | pmda_filter | tee -a $here/$seq.full

echo "=== verify metric values ==="
pminfo -v haproxy 2>&1 \
| $here/src/sortinst | pmda_filter | tee -a $here/$seq.full

pmda_remove
status=0
exit
